/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.control;

import com.scalia.meteocal.entity.Event;
import javax.ejb.Stateless;
import com.scalia.meteocal.weather.ForecastProvider;
import com.scalia.meteocal.weather.StubForecastProvider;
import com.scalia.meteocal.weather.WeatherForecast;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author gabriele
 */
@Stateless
public class ForecastManager {
    ForecastProvider forecastProvider = new StubForecastProvider();
    
    @PersistenceContext
    EntityManager em;
    
    public WeatherForecast setForecastOrScheduleItsUpdate(Event event) {
        WeatherForecast weatherForecast = forecastProvider.getForecast(event.getPlace(), event.getStartTime());
        if (weatherForecast == null) 
            return null;
        
        event.setForecastTemperature(weatherForecast.getTemperature());
        event.setWeatherTypeForecast(weatherForecast.getWeatherType().getName());
        em.merge(event);
        return weatherForecast;
    }
    
    public WeatherForecast getForecast(Event event) {
        WeatherForecast weatherForecast = forecastProvider.getForecast(event.getPlace(), event.getStartTime());
        if (weatherForecast == null) {
            return null;
        }
        return weatherForecast;
    }
    
   
}
